/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bookstore.action;

import com.opensymphony.xwork2.*;
import bookstore.iservice.*;
import bookstore.service.*;
import bookstore.dao.*;
import bookstore.model.*;
import static com.opensymphony.xwork2.Action.ERROR;
import static com.opensymphony.xwork2.Action.LOGIN;
import static com.opensymphony.xwork2.Action.SUCCESS;
import java.io.IOException;
import static java.lang.System.out;
import java.util.Map;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author cjx
 */
public class BookAction extends ActionSupport {

    private IBookService bookService = new BookService();
    private String categoryId;
    private String pageNo;
    private int list_id;
    public String getPageNo() {
        return pageNo;
    }

    public int getList_id() {
        return list_id;
    }

    public void setList_id(int list_id) {
        this.list_id = list_id;
    }

    public void setPageNo(String pageNo) {
        this.pageNo = pageNo;
    }

    public String getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(String categoryId) {
        this.categoryId = categoryId;
    }

    public String execute() {
        ActionContext ctx = ActionContext.getContext();
        Map session = ctx.getSession();
        System.out.println(categoryId);
        bookService.setBookDao(new BsBookDao());
        List<Book> list = bookService.findByCatId(Integer.parseInt(categoryId), Integer.parseInt(getPageNo()),12);
        System.out.print(list.size());
        for(int i=0;i<list.size();i++){
            Book book= (Book)list.get(i);
            System.out.println(book.getAuthor());  
        }  
        session.put("bookList", list);
        session.put("pageNo", pageNo);
        return SUCCESS;

    }
    public String deleteBook()
    {
        System.out.println("11111111111111111111111111");
        System.out.println(list_id);
        System.out.println("11111111111111111111111111");
        ActionContext ctx = ActionContext.getContext();
        Map session = ctx.getSession();
        Book book=((List<Book>)session.get("allList")).get(list_id);
         bookService.setBookDao(new BsBookDao());
         bookService.deleteBook(book.getId());
        return SUCCESS;
    }
    public  String manage()
 {
      ActionContext ctx = ActionContext.getContext();
        Map session = ctx.getSession();
        bookService.setBookDao(new BsBookDao());
  List<Book> allList = bookService.findAllBook(Integer.parseInt(pageNo),8);
   
    session.put("allList", allList);
       return SUCCESS;
    } 
}
